﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Client.Framework
{
    public enum EPlatform
    {
        None,
        Editor,
        Android,
        IPhone,
        StandAlone,
        WebPlayer,
    }

    public static class Utility
    {
        public static void SetActive(GameObject obj, bool bActive)
        {
            if (obj == null) { return; }
            if (obj.activeSelf != bActive)
            {
                obj.SetActive(bActive);
            }
        }

        public static void SetActive(this Component com, bool bActive)
        {
            if (com != null)
            {
                SetActive(com.gameObject, bActive);
            }
        }

        public static void SetBehaviour(this Behaviour behaviour, bool enabled)
        {
            if (behaviour == null) { return; }
            if (behaviour.enabled != enabled)
            {
                behaviour.enabled = enabled;
            }
        }

        public static void SetCollider(this Collider collider, bool enabled)
        {
            if (collider == null) { return; }
            if (collider.enabled != enabled)
            {
                collider.enabled = enabled;
            }
        }

        public static void SetBehaviour<T>(this Transform t, bool enabled) where T : Behaviour
        {
            if (t == null) { return; }
            for (int i = 0; i < t.childCount; i++)
            {
                Transform tf = t.GetChild(i);
                var behaviour = tf.GetComponent<T>();
                SetBehaviour(behaviour, enabled);
            }
        }

        public static void SetChildrenCollider(this Transform t, bool enabled)
        {
            if (t == null) { return; }
            for (int i = 0; i < t.childCount; i++)
            {
                Transform tf = t.GetChild(i);
                var collider = tf.GetComponent<Collider>();
                SetCollider(collider, enabled);
            }
        }

        public static void SetParent(GameObject parent, GameObject child)
        {
            if (child == null) { return; }
            Transform t = child.transform;
            t.localPosition = Vector3.zero;
            t.localRotation = Quaternion.identity;
            t.localScale = Vector3.one;

            if (parent == null) { return; }
            child.layer = parent.layer;
            t.parent = parent.transform;
        }

        /// <summary>
        /// 添加子节点对象
        /// </summary>
        /// <param name="parentObj">父级对象</param>
        /// <param name="prefab">创建对象</param>
        /// <param name="count">数量</param>
        /// <param name="func">回调</param>
        public static void AddChildren(Component parentObj, GameObject prefab,
            int count, System.Action<GameObject, int> func = null)
        {
            Transform parent = parentObj.transform;
            for (int i = parent.childCount; i < count; i++)
            {
                AddChildren(prefab, parent);
            }

            for (int i = 0; i < parent.childCount; i++)
            {
                Transform child = parent.GetChild(i);
                child.gameObject.SetActive(i < count);

                if (i < count)
                {
                    func?.Invoke(child.gameObject, i);
                }
            }
        }

        public static GameObject AddChildren(GameObject prefab, Component parentObj)
        {
            Transform parent = parentObj.transform;
            var go = Object.Instantiate(prefab, parent);
            go.layer = parentObj.gameObject.layer;

            int index = Mathf.Abs(go.GetInstanceID());
            go.name = $"{prefab.name}_{index}";
            go.transform.localPosition = Vector3.zero;
            return go;
        }

        public static T AddMissingComponent<T>(this GameObject go) where T : Component
        {
            if (go == null) { return null; }
            T comp = go.GetComponent<T>();

            if (comp == null)
            {
                comp = go.AddComponent<T>();
            }
            return comp;
        }

        public static string BuildTfPath(this Component com, Transform root)
        {
            if (com != null && root != null)
            {
                return BuildTfPath(com.transform, root);
            }
            return string.Empty;
        }

        public static string BuildTfPath(this Transform tf, Transform root = null)
        {
            var buffer = new StringBuilder();
            if (tf == null) { return buffer.ToString(); }

            while (tf != root)
            {
                if (buffer.Length > 0)
                {
                    buffer.Insert(0, "/");
                }

                buffer.Insert(0, tf.name);
                tf = tf.parent;
            }
            return buffer.ToString();
        }

        /// <summary>
        /// 获取类型名
        /// </summary>
        public static string GetTypeName(this Object com)
        {
            if (com != null)
            {
                var type = com.GetType();
                return type.Name;
            }
            return string.Empty;
        }

        /// <summary>
        /// 根节点相d对于root的localPosition,只针对root没有缩放的情况
        /// </summary>
        /// <param name="t"></param>
        public static Vector3 GetLocalPosition(Transform t)
        {
            if (t == null) { return Vector3.zero; }
            Vector3 pos = t.localPosition;

            for (; ; )
            {
                Transform parent = t.parent;
                if (parent == null) break;
                pos += parent.localPosition;
            }
            return pos;
        }

        public static TValue GetValueByIdx<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, int idx, TValue defalt)
        {
            if (dictionary == null) { return defalt; }
            TValue[] values = Enumerable.ToArray(dictionary.Values);

            if (idx >= 0 && idx < values.Length)
            {
                return values[idx];
            }
            return defalt;
        }

        /// <summary>
        /// 获取目标平台
        /// </summary>
        public static EPlatform GetPlatForm()
        {
#if UNITY_EDITOR
            return EPlatform.Editor;
#elif UNITY_ANDROID
        return EPlatform.Android;
#elif UNITY_IPHONE || UNITY_IOS
        return EPlatform.IPhone;
#elif UNITY_FLASH || UNITY_WEBPLAYER
        return EPlatform.WebPlayer;
#elif UNITY_STANDALONE
        return EPlatform.StandAlone;
#else
        return EPlatform.None;
#endif
        }
    }
}